package com.framework.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.core.log.annotation.AutoLog;
import com.framework.core.result.R;
import com.framework.system.entity.dto.SysMenuAddDTO;
import com.framework.system.entity.dto.SysMenuQueryDTO;
import com.framework.system.entity.dto.SysMenuUpdateDTO;
import com.framework.system.entity.vo.RouteItemVO;
import com.framework.system.entity.vo.SysMenuListVO;
import com.framework.system.entity.vo.SysMenuVO;
import com.framework.system.service.ISysMenuService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单管理
 *
 * @author Kevin
 * @since 2021-06-19
 */
@RestController
@RequestMapping("/system/sysMenu")
@AllArgsConstructor
public class SysMenuController {

    private ISysMenuService sysMenuService;

    /**
     * 分页查询菜单列表
     */
    @GetMapping("/selectSysMenuWithPage")
    @SaCheckPermission("system:sysMenu:selectSysMenuWithPage")
    @AutoLog(module = "菜单管理", title = "分页查询菜单列表")
    public R<Page<SysMenuListVO>> selectSysMenuWithPage(Page reqPage, SysMenuQueryDTO req) {
        return R.success(sysMenuService.selectSysMenuWithPage(reqPage, req));
    }

    /**
     * 新增菜单
     */
    @PostMapping("/addSysMenu")
    @SaCheckPermission("system:sysMenu:addSysMenu")
    @AutoLog(module = "菜单管理", title = "新增菜单")
    public R<Void> addSysMenu(@Validated @RequestBody SysMenuAddDTO req) {
        sysMenuService.addSysMenu(req);
        return R.success();
    }

    /**
     * 修改菜单
     */
    @PostMapping("/updateSysMenu")
    @SaCheckPermission("system:sysMenu:updateSysMenu")
    @AutoLog(module = "菜单管理", title = "修改菜单")
    public R<Void> updateSysMenu(@Validated @RequestBody SysMenuUpdateDTO req) {
        sysMenuService.updateSysMenu(req);
        return R.success();
    }

    /**
     * 删除菜单
     */
    @PostMapping("/removeSysMenu")
    @SaCheckPermission("system:sysMenu:removeSysMenu")
    @AutoLog(module = "菜单管理", title = "删除菜单")
    public R<Void> removeSysMenu(@RequestParam String ids) {
        sysMenuService.removeSysMenu(ids);
        return R.success();
    }

    /**
     * 查看菜单
     */
    @GetMapping("/getSysMenu")
    @SaCheckPermission("system:sysMenu:getSysMenu")
    @AutoLog(module = "菜单管理", title = "查看菜单")
    public R<SysMenuVO> getSysMenu(@RequestParam String id) {
        return R.success(sysMenuService.getSysMenu(id));
    }

    /**
     * 查询菜单下拉树列表排除当前节点以及子节点
     */
    @GetMapping("/selectSysMenuAndExcludeNodeWithOptionTree")
    @AutoLog(module = "菜单管理", title = "查询菜单下拉树列表排除当前节点以及子节点")
    public R<List<Tree<String>>> selectSysMenuAndExcludeNodeWithOptionTree(@RequestParam(required = false) String nodeId) {
        return R.success(sysMenuService.selectSysMenuAndExcludeNodeWithOptionTree(nodeId));
    }

    /**
     * 查询菜单树
     */
    @GetMapping("/selectSysMenuWithTree")
    @AutoLog(module = "菜单管理", title = "查询菜单树")
    public R<List<Tree<String>>> selectSysMenuWithTree() {
        return R.success(sysMenuService.selectSysMenuWithTree());
    }

    /**
     * 查询授权菜单列表
     */
    @GetMapping("/selectGrantMenuWithList")
    @AutoLog(module = "菜单管理", title = "获取菜单集合")
    public R<List<RouteItemVO>> selectGrantMenuWithList() {
        return R.success(sysMenuService.selectGrantMenuWithList());
    }

}

